	var counter = 0;
	var windowId ="";
	var login=false;
	
	chrome.browserAction.onClicked.addListener(function(e) {
	//login=true;
	if(login){
		if(windowId==""){
			chrome.windows.create({'url': createLoginUrl(), 'type': 'popup', 'height': 500, 'width': 400}, function(window) {
				windowId = parseInt(window.id);				
			});
		}else{
			chrome.windows.update(windowId, {focused:true})
		}
	}
	//chrome.browserAction.setPopup({popup:"popup.html"})
	});
	
	function click(e) {
	   window.close();
	}
	function createWindow() {
		if(windowId==""){
			chrome.windows.create({'url': createLoginUrl(), 'type': 'popup', 'height': 500, 'width': 400}, function(window) {
				windowId = parseInt(window.id);				
			});
		}else{
			chrome.windows.update(windowId, {focused:true})
		}
	}
	
	chrome.windows.onRemoved.addListener(function(wId) {
		windowId="";		
	});
	
	document.addEventListener('DOMContentLoaded', function () {
		
		chrome.browserAction.setPopup({popup:"popup.html"})
		
		//$("#frame").attr("src",'http://localhost/public/Portal/templates/login.html'); /*/premium/portal*/
	});
	
	function testAlert(e){
	alert(e.target.id )
	}
	
	function createLoginUrl(){
			var domain = "https://login.salesforce.com";
			var dir = "/services/oauth2/authorize";
			var response_type = "code";
			var client_id = "3MVG99OxTyEMCQ3hGEVHOuhM_VojURIDEvgaF5pBvSpakG77tPOU8V.Wj_TAscc6AzR84o1qs35W3Djz6Y_N_";
			var redirect_uri = "https%3A%2F%2Fportal.ringdna.com%2Fsalesforceoauth%2Fcallback";
			var url = domain + dir + "?response_type=" + response_type + "&client_id=" + client_id +"&redirect_uri=" + redirect_uri+"&display=popup";
			return url;
		}
		
		
		chrome.webRequest.onHeadersReceived.addListener(function(info) {
	    var headers = info.responseHeaders;
		var x_frame_id = -1 ;
        for (var i=headers.length-1; i>=0; --i) {
		
            var header = headers[i].name.toLowerCase();
            if (header == 'x-frame-options' || header == 'frame-options') {
				console.log('test');
			   x_frame_id = i;
            }
        }
				
		//headers.splice(xid, 1);
		if(x_frame_id >0){
			login=false;
			chrome.browserAction.setPopup({popup:"popup.html"})
		}else{
			login=true;
			chrome.browserAction.setPopup({popup:""})
		}
	
        return {responseHeaders: headers};
		},
		{
			urls: [ '*://login.salesforce.com/*','https://na11.salesforce.com/secur/logout.jsp' ], // Pattern to match all http(s) pages
			types: [ 'main_frame','sub_frame' ]
		},
		['blocking', 'responseHeaders']
	);